using System;
using DevExpress.Utils;
using DevExpress.XtraEditors;
using DevExpress.XtraEditors.Controls;
using DevExpress.XtraGrid.Views.Grid;
using E4RPDevExpress10;
using Movements.DataSet;

namespace Movements.Controls
{
    public partial class UserListXtraUserControl : XtraUserControl, IGridView, IList
    {
        private Controller _controller;

        #region Properties

        public Controller Controller
        {
            set
            {
                _controller = value;
                SetupProfileLookupData();
                SetGridDataSource();
                _controller.UserSaved += delegate { SetGridDataSource(); }; 
            }
        }

        #endregion

        public UserListXtraUserControl()
        {
            InitializeComponent();
            gridView.DoubleClick += GridViewDoubleClick;
        }

        #region Private Methods

        private void SetGridDataSource()
        {
            var dataSet = _controller.GetUsers();
            gridControl.DataSource = null;
            gridControl.DataSource = dataSet;
            gridControl.DataMember = dataSet.Utilizador.TableName;            
        }

        private void SetupProfileLookupData()
        {
            perfilUtilizadorDataSet = _controller.GetProfiles();
            profileRepositoryItemLookUpEdit.Columns.Clear();
            profileRepositoryItemLookUpEdit.Columns.AddRange(new[] {
                new LookUpColumnInfo("Id", "Id", 81, FormatType.None, "", false, HorzAlignment.Near),
                new LookUpColumnInfo("Nome", "Nome", 85, FormatType.None, "", true, HorzAlignment.Near)});
            profileRepositoryItemLookUpEdit.DisplayMember = perfilUtilizadorDataSet.Perfil.NomeColumn.ColumnName;
            profileRepositoryItemLookUpEdit.ValueMember = perfilUtilizadorDataSet.Perfil.IdColumn.ColumnName;
            profileRepositoryItemLookUpEdit.DataSource = perfilUtilizadorDataSet.Perfil;
            profileRepositoryItemLookUpEdit.BestFit();
        }

        private void ShowUser(long documentId)
        {
            var window = new UserXtraForm { DocumentId = documentId, Controller = _controller };
            window.ShowDialog();
        }

        #endregion

        #region IGridView

        public GridView GridView()
        {
            return gridView;
        }

        public string ListName
        {
            get { return "UserListUserControl"; }
        }

        #endregion

        public void AddDocument()
        {
            ShowUser(-1);
        }

        public void DeleteDocument()
        {
            throw new NotImplementedException();
        }

        void GridViewDoubleClick(object sender, EventArgs e)
        {
            var user = (UtilizadorDataSet.UtilizadorRow)gridView.GetDataRow(gridView.FocusedRowHandle);
            if (user == null)
                return;
            ShowUser(user.Id);
        }        
    }
}
